{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1. 模型预测\n",
    "1.1 内容描述\n",
    "引用captcha_func文件，加载训练好的模，调用生成验证码数据模型，传入模型进行预测。\n",
    "1.2 代码编写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'captcha'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-431db7c13171>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mcaptcha_func\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mcrack_captcha\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcaptcha_image\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;31m# 加载模型输出节点\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0moutput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgraph\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_tensor_by_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"out:0\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\jupyter\\captcha_func.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mcaptcha\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimage\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mImageCaptcha\u001b[0m \u001b[1;31m#pip install captcha\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mPIL\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mImage\u001b[0m \u001b[1;31m#pillow\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m#c 操作图片，一般使用opencv\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'captcha'"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from captcha_func import *\n",
    "def crack_captcha(captcha_image):\n",
    "    # 加载模型输出节点\n",
    "    output = graph.get_tensor_by_name(\"out:0\")\n",
    "\n",
    "    with tf.Session() as sess:\n",
    "        ckpt = tf.train.get_checkpoint_state('captcha_model')\n",
    "        if ckpt and ckpt.model_checkpoint_path:\n",
    "            checkpoint_path = ckpt.model_checkpoint_path\n",
    "            saver.restore(sess, checkpoint_path)\n",
    "        else:\n",
    "            print('Have no mode')\n",
    "            return\n",
    "        predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)\n",
    "        text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1})\n",
    "        text = text_list[0].tolist()\n",
    "        return vec2text(text)\n",
    "if __name__ == '__main__':\n",
    "    text, image = gen_captcha_text_and_image()\n",
    "\n",
    "    f = plt.figure()\n",
    "    ax = f.add_subplot(111)\n",
    "    ax.text(0.1, 0.9, text, ha='center', va='center', transform=ax.transAxes)\n",
    "    plt.imshow(image)\n",
    "    plt.show()\n",
    "    image = convert2gray(image)\n",
    "    image = image.flatten() / 255\n",
    "    saver = tf.train.import_meta_graph('captcha_model/crack_capcha.model.meta', clear_devices=True)\n",
    "    graph = tf.get_default_graph()\n",
    "    X = graph.get_tensor_by_name(\"X:0\")\n",
    "    Y = graph.get_tensor_by_name(\"Y:0\")\n",
    "    keep_prob = graph.get_tensor_by_name(\"keep_prob:0\")\n",
    "    #调用模型预测\n",
    "    predict_text = crack_captcha(image)\n",
    "\n",
    "    print(\"正确: {}  预测: {}\".format(text, predict_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
